<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Crouse_time;
use App\Models\Squad;
use App\Models\Student;
use App\Models\Student_attendance_record;
use App\Models\Attendance_cycle;
use App\Models\Attendance_report;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class Attendance_reportController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.attendance_report'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.attendance_report'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.attendance_report'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.attendance_report'))
            ->description(trans('admin.detail'))
            ->body(Admin::show(Attendance_report::findOrFail($id), function (Show $show) {
                $show->panel()->tools(function ($tools){
                    $tools->disableDelete();
                    $tools->disableEdit();
                    //$tools->disableList();
                });
                $show->name(trans('admin.attendance_report_name'));
                $show->squad()->name(trans('admin.squad'));
                $show->attendance_cycle()->name(trans('admin.attendance_cycle_id'));
                $show->attendance_date(trans('admin.attendance_date'));
                $show->crouse_time()->name(trans('admin.crouse_time'));
                $show->classroom()->name(trans('admin.classroom'));
                $show->attendance_desc(trans('admin.attendance_desc'));
                
                $show->student_attendance_record('考勤单', function ($line) {
                    $line->setResource('/admin/student_attendance_record');
                    $line->id(trans('admin.ID'))->sortable();
                    $line->student_id(trans('admin.en_name'))->display(function($student_id){
                        return Student::find($this->student_id)->en_name;
                    });
                    $line->attendance_status(trans('admin.attendance_status'))->display(function($attendance_status){
                        if($this->attendance_status==1){
                            return "<span class='label bg-green'>正常</span>";
                        }elseif ($this->attendance_status==2) {
                            return "<span class='label bg-orange'>请假</span>";
                        }elseif ($this->attendance_status==3) {
                            return "<span class='label bg-blue'>迟到</span>";
                        }elseif ($this->attendance_status==4) {
                            return "<span class='label bg-red'>缺勤</span>";
                        }
                    })->sortable();
                    $line->attendance_desc(trans('admin.attendance_desc'))->style('width:600px;word-break:break-all;')->editable('textarea');

                    $line->disableCreateButton();//禁用创建按钮
                    $line->disablePagination();//禁用分页
                    $line->disableFilter();//禁用筛选
                    //禁用批量操作
                    $line->tools(function ($tools) {
                        $tools->batch(function ($batch) {
                            $batch->disableDelete();
                        });
                    });

                    $line->actions(function ($actions) {
                        $actions->disableView();
                        $actions->disableDelete();
                        //$actions->disableEdit();
                        $actions->disableView();
                    });
                    $filename="考勤记录";
                    $line->exporter(new ExcelExpoter($line,$filename));
                });
        }));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Attendance_report::class, function (Grid $grid) {
            $squads = Squad::where('class_teacher','=',Admin::user()->id)->pluck('id')->toArray();

            $grid->model()->wherein('squad_id', $squads);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            
            $grid->attendance_cycle_id(trans('admin.attendance_cycle_id'))->display(function($attendance_cycle_id){
                return Attendance_cycle::find($this->attendance_cycle_id)->name;
            });
            $grid->attendance_date(trans('admin.attendance_date'));
            $grid->crouse_time_id(trans('admin.crouse_time'))->display(function($crouse_time_id){
                return Crouse_time::find($this->crouse_time_id)->name;
            });
            $grid->classroom_id(trans('admin.classroom'))->display(function($classroom_id){
                return Classroom::find($this->classroom_id)->name;
            });
            $grid->name(trans('admin.attendance_report_name'))->sortable();
            $grid->attendance_desc(trans('admin.attendance_desc'))->display(function($attendance_desc) {
                return str_limit($attendance_desc, 30, '...');
            });

            $grid->actions(function ($actions) {
                $actions->disableDelete();//禁用删除按钮
            });
            //禁用导出按钮
            $grid->disableExport();
            // $filename="记录";
            // $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });

            //筛选
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('squad_id', trans('admin.squad'))->select(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name', 'id'));
                    $filter->equal('classroom_id', trans('admin.classroom'))->select(Classroom::all()->pluck('name', 'id'));
                    $filter->equal('attendance_date',trans('admin.attendance_date'))->date();
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('attendance_cycle_id', trans('admin.attendance_cycle_id'))->select(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'));
                    $filter->like('name',trans('admin.attendance_report_name'));
                    $filter->equal('crouse_time_id', trans('admin.crouse_time_id'))->select(Crouse_time::all()->pluck('name', 'id'));
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    { 
        $form = new Form(new Attendance_report());

        $form->display('id', 'ID');
        $form->text('name',trans('admin.attendance_report_name'))->rules('required')->addElementLabelClass('must-input');
        $form->select('squad_id','要录入考勤的班级')->options(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name','id'))->rules('required')->addElementLabelClass('must-input');
        $form->select('attendance_cycle_id','要录入考勤的周期')->options(Attendance_cycle::where('status','=','0')->get()->pluck('name','id'))->rules('required')->addElementLabelClass('must-input');
        $form->select('classroom_id',trans('admin.classroom'))->options(Classroom::all()->pluck('name','id'))->rules('required')->addElementLabelClass('must-input');
        $form->date('attendance_date', trans('admin.attendance_date'))->rules('required')->addElementLabelClass('must-input');
        $form->select('crouse_time_id',trans('admin.crouse_time_id'))->options(Crouse_time::all()->pluck('name','id'))->rules('required')->addElementLabelClass('must-input');
        $form->textarea('attendance_desc',trans('admin.attendance_desc'))->rows(10);
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        //考勤单生成后，自动生成对应的学生考勤记录，学生考勤状态默认1-正常
        $form->saved(function(Form $form){
            $attendance_report_id = $form->model()->id;
            $squad_id = $form->model()->squad_id;
            $student_ids = Student::where('squad_id','=',$squad_id)->pluck('id')->toArray();
            $data = [];
            for ($i=0; $i <count($student_ids) ; $i++) { 
                $data[] = [
                    'attendance_report_id' =>$attendance_report_id,
                    'student_id' =>$student_ids[$i],
                    'attendance_status' =>'1'
                ];
            }
            $res = Student_attendance_record::insert($data);
        });
        return $form;
    }
}
